package model

import (
	"errors"
	"gin/common/types"
	"gin/database"
)

type PanFile struct {
	Id             uint64           `json:"id" gorm:"column:id"`
	PanUserId      uint64           `json:"pan_user_id" gorm:"column:pan_user_id"`
	FsId           uint64           `json:"fs_id" gorm:"column:fs_id"`
	Md5            string           `json:"md5" gorm:"column:md5"`
	ServerFilename string           `json:"server_filename" gorm:"column:server_filename"`
	Category       int              `json:"category" gorm:"column:category;default:6"`
	Path           string           `json:"path" gorm:"column:path"`
	Size           int64            `json:"size" gorm:"column:size"`
	Ctime          types.TstampTime `json:"ctime" gorm:"column:ctime"`
	Mtime          types.TstampTime `json:"mtime" gorm:"column:mtime"`
	IsDir          int              `json:"isdir" gorm:"column:isdir;default:0"`
	Uuid           string           `json:"uuid" gorm:"column:uuid"`
	FromShareId    uint64           `json:"from_share_id" gorm:"column:from_share_id"`
	Depth          int              `json:"depth" gorm:"column:depth"`
	DownloadLink   string           `json:"download_link" gorm:"-"`
}

// 设置表名
func (PanFile) TableName() string {
	return "pan_file"
}

// 插入百度文件数据
func (this *PanFile) Insert() (id uint64, err error) {
	result := Mysql.DB.Create(&this)
	id = this.Id
	if result.Error != nil {
		err = result.Error
		return
	}
	return
}

// 更新百度文件数据
func (this *PanFile) Update() (err error) {
	if this.Id > 0 {
		resultUpdate := Mysql.DB.Save(&this)
		if resultUpdate.Error != nil {
			err = resultUpdate.Error
			return
		}
	} else {
		err = errors.New("ID must exist.")
	}

	return
}

// 通过id查询一条文件数据
func (this *PanFile) FindById(Id int64) (resData PanFile, err error) {
	resultFind := Mysql.DB.Where("id = ?", Id).First(&resData)

	if resultFind.Error != nil {
		err = resultFind.Error
		return
	}

	if resData.Id <= 0 {
		err = errors.New("File does not exist.")
	}
	return
}
